#!/bin/bash
OK=0
WARN=1
CRITICAL=2
UNKNOWN=3
PATH="/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin"

check_item1="afsfile"
check_item2="afscache"
OK_name=''
Fail_name=''
Judge=0

waitnum=`rxdebug localhost 7001|grep -w 'calls waiting for a thread' | awk -F' ' '{print $1}'`
if [ $waitnum -gt 0 ];then
    	echo " afs waiting thread num is $waitnum "
        exit $CRITICAL
else
        echo " $Ok_name are OK "
        exit $OK
fi
exit
currentcpuload=`uptime | awk -F"load average:" '{print $NF}'|awk -F" " '{print $1}'|awk -F"." '{print $1}'`
if [ $currentcpuload -ge 100 ];then
    echo "cpuload is $currentcpuload"
    exit $UNKNOWN
fi

var=$0
program=${var##*/}
runnum=`ps aux | grep -w $program |grep -v "grep" | wc -l `
if [ $runnum -gt 5 ]
then
echo "check_afsfile program number is $runnum"
exit $CRITICAL
fi


if [ -e /afs/ihep.ac.cn/bes3/offline/CalibConst/backup/backup ] 
then
        Ok_name=$check_item1
#       exit $OK
else
        Judge=1
        Fail_name=$check_item1
#       exit $CRITICAL
fi

#to monitor the local cache is available or not
cahcedir=`grep '^CACHEDIR' /etc/sysconfig/afs | awk -F"=" '{print $NF}'`
  touch "$cachedir/testfs" > /dev/null 
  if [ -w "$cachedir/testfs" ]            ###
  then
          Ok_name="${Ok_name} ${check_item2}"
  else
          Judge=1
          Fail_name="${Fail_name} ${check_item2}"
  fi
#TT=`uname -r|grep "2.6.32\|3.10.0"`
#if [ "T$TT" != "T" ]  #mean it is sl6
#then
#  touch /var/cache/afs/testfs > /dev/null 
#  if [ -w /var/cache/afs/testfs ]            ###
#  then
#          Ok_name="${Ok_name} ${check_item2}"
#  else
#          Judge=1
#          Fail_name="${Fail_name} ${check_item2}"
#  fi
#else #mean it is not sl6
#  touch /var/cache/openafs/testfs > /dev/null
#  if [ -w /var/cache/openafs/testfs ]            ###
#  then
#          Ok_name="${Ok_name} ${check_item2}"
#  else
#          Judge=1
#          Fail_name="${Fail_name} ${check_item2}"
#  fi
#fi


if [ $Judge -eq 1 ]
then
        echo "$Fail_name  wrong"
        exit $CRITICAL
else
        echo " $Ok_name are OK "
        exit $OK
fi
